package util;

import java.util.Map;

/**
 * 组装HQL
 * @author 18信管2_7组
 */
public class HqlUtil {

	/**
	 * 对于"入住登记-退房办理"页面，记录搜索表单提交的参数组装HQL语句
	 * 表单数据有订单号orderNumber、房间类型categoryid、入住时间checkin_time
	 * 房间号roomid、姓名customerName、联系方式tele、状态state
	 */
	public static String assembleHqlForCheckinForm(Map<String,String[]> map) {
		StringBuffer buffer =new StringBuffer();
		for(Map.Entry<String, String[]> entry:map.entrySet()) {
			switch(entry.getKey()) {
			case "orderNumber": if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) buffer.append("or i.sales.orderNumber ='"+entry.getValue()[0]+"' "); break;
			case "categoryid": if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) buffer.append("or i.sales.category.categoryid ='"+entry.getValue()[0]+"' "); break;
			case "checkin_time": if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) buffer.append("or i.checkin_time like '"+entry.getValue()[0]+"%' "); break;
			case "no": if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) buffer.append("or i.room.no ='"+entry.getValue()[0]+"' "); break;
			case "state": if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) buffer.append("or i.state ='"+entry.getValue()[0]+"' "); break;
			case "customerName": 
				if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) 
					buffer.append("or i.itemid IN( select d.salesLineItem.itemid From ItemDescription d where d.customerName LIKE '%"+entry.getValue()[0]+"%' )"); 
				break;
			case "tele": 
				if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) 
					buffer.append("or i.itemid IN( select d.salesLineItem.itemid From ItemDescription d where d.tele ='"+entry.getValue()[0]+"') "); 
				break;
			}
		}
		buffer.delete(0, 3);//删除最开始的'or '
		return "from SalesLineItem i where "+buffer.toString();
	}
	
	/**
	 * 对于"预定管理"页面，预定搜索表单提交的参数组装HQL语句
	 * 表单数据有预定房间类型categoryid、预计入住时间start_time、预计退房时间end_time
	 * 姓名userName、联系方式tele、预定时间reserveTime、状态state
	 */
	public static String assembleHqlForReserveForm(Map<String,String[]> map) {
		StringBuffer buffer =new StringBuffer();
		for(Map.Entry<String, String[]> entry:map.entrySet()) {
			switch(entry.getKey()) {
			case "categoryid": if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) buffer.append("or r.category.categoryid ='"+entry.getValue()[0]+"' "); break;
			case "start_time": if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) buffer.append("or r.start_time like '"+entry.getValue()[0]+"%' "); break;
			case "end_time": if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) buffer.append("or r.end_time like '"+entry.getValue()[0]+"%' "); break;
			case "userName": if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) buffer.append("or r.userName ='"+entry.getValue()[0]+"' "); break;
			case "tele": if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) buffer.append("or r.tele ='"+entry.getValue()[0]+"' "); break;
			case "reserveTime": if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) buffer.append("or r.reserveTime like '"+entry.getValue()[0]+"%' "); break;
			case "state": if(entry.getValue()[0]!=null&&entry.getValue()[0].length()>0) buffer.append("or r.state ='"+entry.getValue()[0]+"' "); break;
			}
		}
		buffer.delete(0, 3);//删除最开始的'or '
		return "from Reserve r where "+buffer.toString();
	}
}
